package com.foreveross.dao.imp;

import java.util.List;

import org.springframework.stereotype.Component;

import com.foreveross.common.SQLUtil;
import com.foreveross.dao.TenementDAO;
import com.foreveross.vo.SysUser;
import com.foreveross.vo.Tenement;

@Component("tenementDAO")
public class TenementDAOImp extends SQLUtil implements TenementDAO{

	@Override
	public List<Tenement> findTenementList(String house_id, String name, String phone,
			int pageIndex, int pageSize, String userId) throws Exception {
		StringBuffer queryString = new StringBuffer(
				"select model from Tenement model where 1=1 ");
		if (!"".equals(house_id) && null != house_id) {
			queryString.append(" and model.houseId.id = '"+house_id+"'");
		}
		if (!"".equals(name) && null != name) {
			queryString.append(" and model.name like '%" + name + "%'");
		}
		if (!"".equals(phone) && null != phone) {
			queryString.append(" and model.phone like '%" + phone + "%'");
		}
		this.addAuthorityStr(queryString, userId, "houseId.id");
		queryString.append(" order by model.id desc");
		return (List<Tenement>) findObjectAll(queryString.toString(), pageIndex,
				pageSize);
	}

	@Override
	public long getTenementListCount(String house_id, String name, String phone, String userId)
			throws Exception {
		StringBuffer queryString = new StringBuffer(
				"select count(*) from Tenement model where 1=1 ");
		if (!"".equals(house_id) && null != house_id) {
			queryString.append(" and model.houseId.id = '"+house_id+"'");
		}
		if (!"".equals(name) && null != name) {
			queryString.append(" and model.name like '%" + name + "%'");
		}
		if (!"".equals(phone) && null != phone) {
			queryString.append(" and model.phone like '%" + phone + "%'");
		}
		this.addAuthorityStr(queryString, userId, "houseId.id");
		return (Long) findObjectAll(queryString.toString()).get(0);
	}

	@Override
	public void deleteTenement(String tenementId) throws Exception {
		 final String sqlv = "delete from TenementHouse t where t.tenement.id='" + tenementId + "'";
		 updateObejctByHql(sqlv); 
		 final String sql = "delete from Tenement t where t.id='" + tenementId + "'";
		 updateObejctByHql(sql); 
	}
	 
	@Override
	public void deleteTenementHouse(String tenementId) throws Exception {
		 final String sql = "delete from TenementHouse t where t.tenement.id='" + tenementId + "'";
		 updateObejctByHql(sql); 
	}

}
